//
//  RecommendViewModel.swift
//  swiftDemo
//
//  Created by Gerry on 2021/11/29.
//

import UIKit
import Alamofire

class RecommendViewModel: NSObject {
    lazy var hotCates : [HotCateModel] = [HotCateModel]()
    lazy var bannerImages : [BannerModel] = [BannerModel]()
    
    private var group : DispatchGroup = DispatchGroup()
}

extension RecommendViewModel {
    func requestData( responseCallback : @escaping(_ : Bool, _ : String?) -> Void) {

        let hotCateModel1 = HotCateModel()
        // http://capi.douyucdn.cn/api/v1/getbigDataRoom?time=1637507283
        group.enter()
        Network.request(type: .GET, urlString: kbigDataRoomUrl, parameters: ["time" : NSDate.getCurrentTimeString()]) { result, isSuccess in
            guard let datas = result as? Array<Dictionary<String, NSObject>> else {
                return
            }
            
            let list = NSArray.yy_modelArray(with: RoomItem.self, json: datas)
            hotCateModel1.tag_name = "大数据"
            for item in list as! [RoomItem] {
                hotCateModel1.room_list?.append(item)
            }
            
            self.group.leave()
        }
        
        let hotCateModel2 = HotCateModel()
        // http://capi.douyucdn.cn/api/v1/getVerticalRoom?limit=4&offset=0&time=1637507283
        group.enter()
        let parameters = ["limit": "4", "offset" : "0", "time" : NSDate.getCurrentTimeString()]
        Network.request(type: .GET, urlString: kVerticalRoomUrl, parameters: parameters) { result, isSuccess in
            guard let datas = result as? Array<Dictionary<String, NSObject>> else {
                print("error")
                responseCallback(false, "request error")
                return
            }
            
            let list = NSArray .yy_modelArray(with: RoomItem.self, json: datas)
            hotCateModel2.tag_name = "颜值"
            for item in list as! [RoomItem] {
                hotCateModel2.room_list?.append(item)
            }
            self.group.leave()
        }
        
        
        group.enter()
        // http://capi.douyucdn.cn/api/v1/getHotCate?limit=4&offset=0&time=1637507283
        Network.request(type: .GET, urlString: kHotCateUrl, parameters: parameters) { (result, isSuccess) in
            guard let datas = result as? [[String : NSObject]] else {
                responseCallback(false, "request error")
                print("error")
                return
            }
            
            let list = NSArray .yy_modelArray(with: HotCateModel.self, json: datas)
            for item in list as! [HotCateModel] {
                self.hotCates.append(item)
            }
            
            self.group.leave()
        }
        
        group.notify (queue: DispatchQueue.main) {
            self.hotCates.insert(hotCateModel2, at: 0)
            self.hotCates.insert(hotCateModel1, at: 0)
            responseCallback(true, nil)
        }
    }
    
    func requestBanner(responseCallback : @escaping(_ : Bool, _ : String?) -> Void) {
        let urlString = kPrefixUrl_dy_tv + kSlide_6
        let params = ["time" : NSDate.getCurrentTimeString()]
        var headers = HTTPHeaders.init()
        headers.add(name: "user_name", value: "gerry")
        Network.baseRequest(type: .get, url: urlString, parameters: params, headers: headers) { result, isSuccess in
            guard let list = NSArray.yy_modelArray(with: BannerModel.self, json: result ?? "") as? [BannerModel] else {
                return
            }
            self.bannerImages = list
            responseCallback(true, "")
        }
    }
}
